package com.molichuxing.services.infrastructure.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.molichuxing.services.infrastructure.bean.OrderStatusFlowBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.OrderStatusFlowMapper;
import com.molichuxing.services.infrastructure.dto.request.create.OrderStatusFlowCreateDto;
import com.molichuxing.services.infrastructure.dto.response.OrderDealerDetailStatusDto;
import com.molichuxing.services.infrastructure.dto.response.OrderExclusivelStatusFlowDto;
import com.molichuxing.services.infrastructure.dto.response.OrderStatusFlowDto;
import com.molichuxing.services.infrastructure.dto.response.OrderSubscribeStatusFlowDto;
import com.molichuxing.services.infrastructure.service.OrderStatusFlowService;

/**
 * <p>
 * 订单状态流转 服务实现类
 * </p>
 *
 * @author renzhengyu
 * @since 2019-08-12
 */
@Service("orderStatusFlowService")
public class OrderStatusFlowServiceImpl implements OrderStatusFlowService {
	private static final Logger LOGGER = LoggerFactory.getLogger(OrderStatusFlowServiceImpl.class);

	@Autowired
	private OrderStatusFlowMapper orderStatusFlowMapper;

	/**
	 * 获取采购单信息详情 - 状态流转
	 * 
	 * @param orderCode
	 *            订单号
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<OrderDealerDetailStatusDto> getOrderDealerDetailStausFlowList(Long orderCode) throws Exception {
		return getDealerOrderStatusFlow(orderCode);
	}

	/**
	 * 新增订单状态流转信息表
	 * 
	 * @param orderStatusFlowCreateDto
	 *            新增dto
	 * @return
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public Boolean createOrderStatusFlow(OrderStatusFlowCreateDto orderStatusFlowCreateDto) {
		OrderStatusFlowBean bean = new OrderStatusFlowBean();
		BeanUtils.copyProperties(orderStatusFlowCreateDto, bean);

		return (orderStatusFlowMapper.create(bean) > 0) ? true : false;
	}

	/**
	 * 订阅里程订单流转信息
	 * 
	 * @param orderCode
	 *            订单号
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<OrderSubscribeStatusFlowDto> getSubscribeOrderStatusFlow(Long orderCode) throws Exception {
		return getOrderStatusFlow(orderCode, OrderSubscribeStatusFlowDto.class);
	}

	/**
	 * 专属里程订单流转信息
	 * 
	 * @param orderCode
	 *            订单号
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<OrderExclusivelStatusFlowDto> getExclusivelOrderStatusFlow(Long orderCode) throws Exception {
		return getOrderStatusFlow(orderCode, OrderExclusivelStatusFlowDto.class);
	}

	/**
	 * 经销商订单流转信息
	 * 
	 * @param orderCode
	 *            订单号
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<OrderDealerDetailStatusDto> getDealerOrderStatusFlow(Long orderCode) throws Exception {
		return getOrderStatusFlow(orderCode, OrderDealerDetailStatusDto.class);
	}

	@Override
	public int modifyDealerOrderStatusFlow(Long orderCode, Integer status) {
		return orderStatusFlowMapper.modifyDealerOrderStatusFlow(orderCode,status);
	}

	@Override
	public int modifyDeleteDealerOrderStatusFlow(Long orderCode, Integer status) {
		return orderStatusFlowMapper.modifyDeleteDealerOrderStatusFlow(orderCode,status);
	}

	/**
	 * 订单流转信息
	 * 
	 * @param orderCode
	 *            订单号
	 * @param clz
	 *            class
	 * @return
	 * @throws Exception
	 */
	private <T extends OrderStatusFlowDto> List<T> getOrderStatusFlow(Long orderCode, Class<T> clz) throws Exception {
		return Convert.toOrderStatusFlowDto(orderStatusFlowMapper.getListByOrderCode(orderCode), clz);
	}

}
